package org.artifact.builder.design;

import org.artifact.builder.enums.BuilderDatabaseEnum;
import org.w3c.dom.Element;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@AllArgsConstructor
public class DesignColumn {
	/** 命名 */
	private String columnName;
	/** 字段名 */
	private String fieldName;
	/** 类型 */
	private String type;
	/** 主键 */
	private boolean pk;
	/** 注释 */
	private String remark;
	/** 自动 */
	private boolean auto;
	/** 默认值 */
	private String defaults;
	/** 数据库类型 */
	private String dbType;
	/** 外键 */
	private String fk;
	
	public DesignColumn(Element column,DesignConfig designConfig) {
		this.columnName = column.getAttribute("name");
		
		if (designConfig.getType() == BuilderDatabaseEnum.mongodb && StrUtil.equals(this.columnName, "_id")) {
			this.fieldName = this.columnName;
		}else {
			// 1.转驼峰
			this.fieldName = StrUtil.toCamelCase(this.columnName);
			// 2. 首字母小写
			this.fieldName = StrUtil.lowerFirst(this.fieldName);
		}
		
		this.type = column.getAttribute("type");
		this.pk = Boolean.valueOf(column.getAttribute("pk"));
		this.remark = column.getAttribute("remark");
		this.auto = Boolean.valueOf(column.getAttribute("auto"));
		this.defaults = column.getAttribute("defaults");
		this.dbType = column.getAttribute("dbType");
		this.fk = column.getAttribute("fk");
	}
}
